/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MCDPPacket.h"

//---------------------------------------------------------------------------

MCDPPacket::MCDPPacket(void)
{
	Initialize();
}

MCDPPacket::~MCDPPacket(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MCDPPacket::CleanUp(void)
{
	xValuesList.Clear();
}

//---------------------------------------------------------------------------

void MCDPPacket::Initialize(void)
{
	xVersion = 0x00;
	xTTL     = 0x00;
	xChkSum  = 0x0000;
	xValuesList.Clear();
}

//---------------------------------------------------------------------------

void MCDPPacket::LoadFromPointer(MCDPPacket* recordIn)
{
	MTLVRecord *rec;
	int i, cnt;

	Initialize();
	if(recordIn==NULL) return;

	xVersion = recordIn->xVersion;
	xTTL     = recordIn->xTTL;
	xChkSum  = recordIn->xChkSum;

	cnt = recordIn->xValuesList.GetSize();
	if(cnt==0) return;

	for(i=0; i<cnt; i++){
		rec = recordIn->xValuesList.GetByIdx(i);
		if(rec!=NULL)
			xValuesList.Add(rec);
	}
}

//---------------------------------------------------------------------------

bool MCDPPacket::IsEmpty(void)
{
	return xVersion==0 ? true : false;
}

bool MCDPPacket::IsEqualWith(MCDPPacket* recordIn)
{
	MTLVRecord *rec1;
	MTLVRecord *rec2;
	bool theSame;
	int i, cnt;

	if(recordIn==NULL) return false;

	if(xVersion != recordIn->xVersion) return false;
	if(xTTL     != recordIn->xTTL)     return false;
	if(xChkSum  != recordIn->xChkSum)  return false;

	cnt = xValuesList.GetSize();
	if(recordIn->xValuesList.GetSize()!=cnt) return false;

	theSame = true;
	for(i=0; i<cnt && theSame; i++){
		rec1 = xValuesList.GetByIdx(i);
		rec2 = xValuesList.GetByIdx(i);
		if(rec1==NULL){
			if(rec2==NULL)
				theSame = false;
		}
		else{
			if(!rec1->IsEqualWith(rec2))
				theSame = false;
		}
	}

	return theSame;
}

//---------------------------------------------------------------------------
